//
//  MovieTableViewController.m
//  UI-13-DouBan--
//
//  Created by lanou3g on 15/8/25.
//  Copyright (c) 2015年 自由科技. All rights reserved.
//

#import "MovieTableViewController.h"
#import "MovieCell.h"
#import "JSONKit.h"

#import "Movie.h"

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"

#import "MovieDetailCollectionViewController.h"

#define kURL @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"
#define kDetailURL @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php"

@interface MovieTableViewController ()

@property (nonatomic, retain) NSMutableArray *muArray;

@end
static NSString *identifier = @"Movie";
@implementation MovieTableViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.title = @"电影";
        self.view.backgroundColor= [UIColor purpleColor];
        self.tabBarItem.image = [UIImage imageNamed:@"movie"];
    }
    return self;
}



- (void)viewWillAppear:(BOOL)animated{
//    接口 http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php
    
    NSURL *url = [NSURL URLWithString:kURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSData *dataa = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSDictionary *dataDic = [dataa objectFromJSONData];
    
    _muArray = [NSMutableArray array];
    for (NSDictionary *dic in [dataDic objectForKey:@"result"]) {
        Movie *model = [[Movie alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_muArray addObject:model];
    }
    
    
    //九宫格视图
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection"] style:UIBarButtonItemStylePlain target:self action:@selector(jiuGongGe:)];
    
}

- (void)jiuGongGe:(UIBarButtonItem *)sender{
    MovieDetailCollectionViewController *cvvv = [[MovieDetailCollectionViewController alloc] init];
    UINavigationController *NAVVV = [[UINavigationController alloc] initWithRootViewController:cvvv];
    cvvv.dataArray = [NSMutableArray arrayWithArray:_muArray];
    cvvv.modalTransitionStyle = 4;
#warning push九宫格
    [self.navigationController pushViewController:cvvv animated:YES];
//    [self.navigationController presentViewController:NAVVV animated:YES completion:nil];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[MovieCell class] forCellReuseIdentifier:identifier];
    self.navigationItem.title = @"电影";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _muArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    Movie *movie = _muArray[indexPath.row];
    
    cell.movieNameLabel.text = movie.movieName;
    
    //添加观察者
    if (movie.myImage == nil && movie.isLoading == NO) {
        cell.iconImgView.image = [UIImage imageNamed:@"picholder"];
        [movie loadImage];
        [movie addObserver:self forKeyPath:@"myImage" options:NSKeyValueObservingOptionNew context:(__bridge void*)indexPath];
    } else {
        cell.iconImgView.image = movie.myImage;
    }
    
    return cell;
}
//移除观察者
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    //拿到新图片资源
    UIImage *tempImg = change[NSKeyValueChangeNewKey];
    //判断当前图片
    if ([tempImg isEqual:[NSNull null]]) {
        return;
    }
    //拿到当前图片所在的路径
    NSIndexPath *indexPatn = (__bridge NSIndexPath *)context;
    //拿到当前正在显示的cell的indexpath
    NSArray *array = [self.tableView indexPathsForVisibleRows];
    //判断indexpath是否在正在显示的rows数组里面
    if ([array containsObject:indexPatn]) {
        MovieCell *newcell = (MovieCell *)[self.tableView cellForRowAtIndexPath:indexPatn];
        //换图
        newcell.iconImgView.image = tempImg;
        
        
        //更新cell
        [self.tableView reloadRowsAtIndexPaths:@[indexPatn] withRowAnimation:UITableViewRowAnimationLeft];
    }
    //移除观察者
    [object removeObserver:self forKeyPath:@"myImage"];
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc] init];
    
// http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=216609
    Movie *movie = _muArray[indexPath.row];
    
//    NSURL *URL = [NSURL URLWithString:kDetailURL];
//    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
    NSString *body = [NSString stringWithFormat:@"%@?movieId=%@",kDetailURL, movie.movieId];
    
    NSURL *url = [NSURL URLWithString:body];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dic111 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSDictionary *resultDic = [dic111 objectForKey:@"result"];
    
    Movie *mmm = [[Movie alloc] init];
    [mmm setValuesForKeysWithDictionary:resultDic];
//    mmm.myImage = movie.myImage;
    
//    movieDetailVC.movieDetailView.ratingLabel.text = mmm.rating;
    movieDetailVC.movieModel = mmm;
    
    [self.navigationController pushViewController:movieDetailVC animated:YES];
}




@end
